package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Item;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.domain.TbItem;
import com.hmall.item.service.ItemService;
import com.hmall.item.mapper.ItemMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 肉丸子
* @description 针对表【tb_item(商品表)】的数据库操作Service实现
* @createDate 2022-07-22 18:38:51
*/
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, TbItem>
    implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    /**
     *  分页查询数据
     */
    @Override
    public PageDTO<Item> getItemByPage(Integer current, Integer pageSize) {
        //1.创建Page对象,提供要查询的页码和每页显示的条数
        Page<TbItem> page = new Page<>(current, pageSize);
        //2.分页查询
        itemMapper.selectPage(page, null);
        // 获取当前页的数据信息
        List<TbItem> tbItems = page.getRecords();
        ArrayList<Item> items = new ArrayList<>();
        for (TbItem tbItem : tbItems) {
            Item item = new Item();
            BeanUtils.copyProperties(tbItem,item);
            items.add(item);
        }
        // 获取总条数
        long total = page.getTotal();
        return new PageDTO<>(total,items);
    }

    /**
     * 根据id查询商品
     */
    @Override
    public Item getItermById(Long id) {
        TbItem tbItem = itemMapper.selectById(id);
        Item item = new Item();
        BeanUtils.copyProperties(tbItem,item);
        return item;
    }

    /**
     * 新增商品
     */
    @Override
    public void insertItem(Item item) {
        TbItem tbItem = TbItem.builder()
                .id(IdWorker.getId())
                .name(item.getName())
                .price(item.getPrice())
                .stock(item.getStock())
                .category(item.getCategory())
                .brand(item.getBrand())
                .spec(item.getSpec())
                .sold(0)
                .commentCount(0)
                .status(1)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        itemMapper.insert(tbItem);

    }

    /**
     * 商品上架、下架
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        TbItem tbItem = TbItem.builder()
                .id(id)
                .status(status)
                .updateTime(new Date())
                .build();
        itemMapper.updateById(tbItem);
    }

    /**
     * 修改商品
     */
    @Override
    public void updateItem(Item item) {
        TbItem tbItem = TbItem.builder()
                .id(item.getId())
                .name(item.getName())
                .price(item.getPrice())
                .stock(item.getStock())
                .category(item.getCategory())
                .brand(item.getBrand())
                .spec(item.getSpec())
                .sold(item.getSold())
                .commentCount(item.getCommentCount())
                .status(item.getStatus())
                .createTime(null)
                .updateTime(new Date())
                .build();
        itemMapper.updateById(tbItem);
    }
}




